package com.lb.shopguide.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.lb.baselib.base.DialogBase;
import com.lb.shopguide.R;
import com.lb.shopguide.ui.fragment.guide.FragmentEnterPhone;

import butterknife.OnClick;

/**
 * Created by kang on 2017/9/8.
 */

public class DialogOpenActivitySuccess extends DialogBase implements View.OnClickListener {
    private TextView tvCount;
    private TextView tvGotoActivity;
    private TextView tvBackHome;

    private MyCountDown countDown;

    public static DialogOpenActivitySuccess getDialogOpenActivitySuccess() {
        return new DialogOpenActivitySuccess();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View mFragmentView = inflater.inflate(R.layout.dialog_open_activity_success, container, false);
        initData();
        initView(mFragmentView);

//        setDialogPosition();
        bindData();
        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(STYLE_NO_TITLE, R.style.Theme_Base_AppCompat_Dialog_Light);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (countDown != null) {
            countDown.cancel();
        }
    }

    private void initData() {

    }

    private void initView(View view) {
        tvCount = view.findViewById(R.id.tv_count);
        tvBackHome = view.findViewById(R.id.tv_back_home);
        tvGotoActivity = view.findViewById(R.id.tv_goto_activity);

        tvBackHome.setOnClickListener(this);
        tvGotoActivity.setOnClickListener(this);
    }

    private void bindData() {
        countDown();
    }

    private void countDown() {
        if (countDown == null) {
            countDown = new MyCountDown(3000, 1000);
        }
        countDown.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back_home:
                if (mOnDialogClickListener != null) {
                    DialogOpenActivitySuccess.this.dismiss();
                    mOnDialogClickListener.backToHome();
                }
                break;
            case R.id.tv_goto_activity:
                if (mOnDialogClickListener != null) {
                    DialogOpenActivitySuccess.this.dismiss();
                    mOnDialogClickListener.gotoMyActivity();
                }
                break;
        }
    }

    class MyCountDown extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvCount.setText(millisUntilFinished / 1000 + "s后进入我的活动");
        }

        @Override
        public void onFinish() {
            tvCount.setText("正在进入我的活动");
            if (mOnDialogClickListener != null) {
                DialogOpenActivitySuccess.this.dismiss();
                mOnDialogClickListener.gotoMyActivity();
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    public interface OnDialogClickListener {
        void backToHome();

        void gotoMyActivity();
    }

    private OnDialogClickListener mOnDialogClickListener;

    public void setOnDialogClickListener(OnDialogClickListener listener) {
        mOnDialogClickListener = listener;
    }
}
